#include <new.h>
#include "base/at_exit.h"
#include "base/process_util.h"
#include "proxy.h"
#include "work_context.h"

#pragma comment(lib, "Psapi.lib")
#pragma comment(lib, "Dbghelp.lib")
#pragma comment(lib, "base.lib")
#pragma comment(lib, "ipc.lib")

#define DLLEXPORT __declspec(dllexport)

namespace {
#pragma optimize("", off)
void InvalidParameter(const wchar_t* expression, const wchar_t* function,
	const wchar_t* file, unsigned int line,
	uintptr_t reserved) {
		__debugbreak();
		_exit(1);
}

void PureCall() {
	__debugbreak();
	_exit(1);
}
#pragma optimize("", on)

base::AtExitManager exit_manager;
}  // namespace

scoped_ptr<WorkContext> work_context;

DLLEXPORT int __stdcall ProxyMain(HINSTANCE instance,	IWorkProvider** provider) {
	_set_invalid_parameter_handler(InvalidParameter);
	_set_purecall_handler(PureCall);
	_set_new_mode(1);
	base::EnableTerminationOnHeapCorruption();

	assert(!work_context.get());
	work_context.reset(new WorkContext());
	*provider = work_context.get();

	return 1;
}